<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin', 'admin', 0);

class manage extends admin{
	
	private $db;
	
	public $siteid;
	
	public $admin_userid;
	
	function __construct($param) {
		parent::__construct();
		$this->db = pc_base::load_model('comment_nsr_model');
		$this->siteid = get_siteid();
		$this->admin_userid = param::get_cookie('userid');//获取当前登录的管理员ID
	}
	
	public function init() {
		//获取管理员真实姓名数据
		$admin_model = pc_base::load_model('admin_model');
		$adminInfo = $admin_model->getRealnameInfo();
		
		$where = "";
		$where .= "status != 4 and siteid = ".$this->siteid;
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$comment_type = isset($_GET['comment_type']) ? intval($_GET['comment_type']) : 1;
		switch ($comment_type){
			case '1' ://筛选资讯评论
				$this->source_db = pc_base::load_model('news_nsr_model');//加载资讯模型
				$where .= " and source_type = ".$comment_type;
				break;
			case '2' ://筛选图集评论
				$this->source_db = pc_base::load_model('picture_nsr_model');//加载图集模型
				$where .= " and source_type = ".$comment_type;
				break;
			case '3' ://筛选视频评论
				$this->source_db = pc_base::load_model('video_nsr_model');//加载视频模型
				$where .= " and source_type = ".$comment_type;
				break;
			case '4' ://筛选人才评论
				$this->source_db = pc_base::load_model('talent_model');//加载人才模型
				$where .= " and source_type = ".$comment_type;
				break;
			case '5' ://筛选赛事评论
				$this->source_db = pc_base::load_model('contest_model');//加载赛事模型
				$where .= " and source_type = ".$comment_type;
				break;
			case '6' ://筛选活动评论
				$this->source_db = pc_base::load_model('contest_model');//加载赛事模型（is_activity区分是否为活动）
				$where .= " and source_type = ".$comment_type;
				break;
			case '7' ://筛选课程评论
				$this->source_db = pc_base::load_model('course_nsr_model');//加载课程模型
				$where .= " and source_type = ".$comment_type;
				break;
			case '8' ://筛选机构评论
				$this->source_db = pc_base::load_model('agency_nsr_model');//加载机构模型
				$where .= " and source_type = ".$comment_type;
				break;
				
		}
		if (isset($_GET['search'])) {
			$keywords= trim($_GET['keyword']);
			$comment_con = trim($_GET['comment_con']);
			$start_time = $_GET['start_time'];
			$end_time = $_GET['end_time'];
			if(!empty($keywords)){
				$where .= " and username like '%$keywords%'";
			}
			if (!empty($comment_con)) {
				$where .= " and content like '%$comment_con%'";
			}
			if(!empty($start_time)){
				$where .= " and create_time > '$start_time'";
			}
			if(!empty($end_time)){
				$where .= " and create_time < '$end_time'";
			}
		}
		$comment_info = $this->db->listinfo($where,'id DESC',$page,10);
		$pages = $this->db->pages;
		pc_base::load_sys_class('form','',0);
		pc_base::load_sys_class('format','',0);
		include $this->admin_tpl('comment_nsr_listinfo');
	}
	
	public function del() {
		if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
			showmessage(L('message_not_exits'), HTTP_REFERER);
		}
		$comment_type = isset($_GET['comment_type']) ? intval($_GET['comment_type']) : 1;
		$id = $_REQUEST['id'];
		if (is_array($id)) {
			foreach ($id as $k => $v) {
				if (intval($v)) {
					$id[$k] = intval($v);
				} else {
					unset($id[$k]);
				}
			}
			$id = implode('\',\'', $id);
		}
		$result = $this->db->update(array('status' => '4'),"id in ('$id')");

		//记录行为日志
        $id = safe_replace($id);
        $log_content = '<span style="color:red;">批量删除评论</span>[评论id：('.$id.')]';
        $this->manage_admin_log($log_content);

		if($result){
			showmessage(L('del_success'),'?m=comment_nsr&c=manage&a=init&comment_type='.$comment_type);
		} else {
			showmessage(L('del_error'),HTTP_REFERER);
		}
	}
	
	public function check() {
		if (! isset($_GET['id']) || empty($_GET['id'])) {
			showmessage(L('message_not_exits'), HTTP_REFERER);
		}
		$comment_type = isset($_GET['comment_type']) ? intval($_GET['comment_type']) : 1;
		$id = $_GET['id'];
		if ($_GET['pass'] == 0) {
			$data['status'] = 1;
		} elseif($_GET['pass'] == 1){
			$data['status'] = 2;
		}
		$data['checked_by'] = $this->admin_userid;
		$data['checked_time'] = date('Y-m-d H:i:s',time());
		$result = $this->db->update($data,array('id'=>$id));
		if($result){
			showmessage(L('checked_success'),'?m=comment_nsr&c=manage&a=init&comment_type='.$comment_type);
		} else {
			showmessage(L('checked_error'),HTTP_REFERER);
		}
	}
	
	//批量审核通过、不通过：flag=1表示通过；flag=2表示不通过 2017-06-28 11:20:12 吴发顶
	public function check_all() {
		if (! isset($_GET['cids']) || empty($_GET['cids'])) {
			showmessage(L('message_not_exits'), HTTP_REFERER);
		}
		$comment_type = isset($_GET['comment_type']) ? intval($_GET['comment_type']) : 1;
		$id = $_GET['cids'];
		$where = ' id in ('.$id.')';
		if ($_GET['flag'] == 1) {
			$log_content = '<span style="color:blue;">评论审核通过</span>';
			$data['status'] = 1;
		} elseif($_GET['flag'] == 2){
			$data['status'] = 2;
			$log_content = '<span style="color:blue;">评论审核不通过</span>';
		}
		$data['checked_by'] = $this->admin_userid;
		$data['checked_time'] = date('Y-m-d H:i:s',time());
		$result = $this->db->update($data,$where);
        if($result){
        	//记录行为日志
        	$id = safe_replace($id);
        	$log_content .= '[评论id：('.$id.')]';
        	$this->manage_admin_log($log_content);

            echo  json_encode(array("status"=>true,"code"=>200,"msg"=>"操作成功！"));
        }else{
            echo  json_encode(array("status"=>false,"code"=>1000,"msg"=>"操作失败，请稍后重试！"));
        }
	}
}